-- load public variables into local
local settings = ShaguDPS.settings
local window = ShaguDPS.window
local parser = ShaguDPS.parser

local config = ShaguDPS.config
local textures = ShaguDPS.textures
local playerClasses = ShaguDPS.playerClasses

-- Load settings on Login
settings:RegisterEvent("PLAYER_ENTERING_WORLD")
settings:SetScript("OnEvent", function()
  if ShaguDPS_Config then
    for k, v in pairs(ShaguDPS_Config) do
      config[k] = v
    end
  end

  ShaguDPS_Config = config
  window.Refresh(true)
end)

-- Provide Slash Commands
SLASH_SHAGUMETER1, SLASH_SHAGUMETER2 = "/shagudps", "/sdps"
SlashCmdList["SHAGUMETER"] = function(msg, editbox)

  local function p(msg)
    if (DEFAULT_CHAT_FRAME) then
      DEFAULT_CHAT_FRAME:AddMessage(msg)
    end
  end

  if GetLocale() == "zhCN" then
    if (msg == "" or msg == nil) then
      p("|cffffcc00Shagu|cffffffffDPS:")
      p("  /sdps visible " .. config.visible .. " |cffcccccc- 显示主窗口|r")
      p("  /sdps width " .. config.width .. " |cffcccccc- 数据条宽度|r")
      p("  /sdps height " .. config.height .. " |cffcccccc- 数据条高度|r")
      p("  /sdps spacing " .. config.spacing .. " |cffcccccc- 数据条间距|r")
      p("  /sdps bars " .. config.bars .. " |cffcccccc- 显示的数据条数|r")
      p("  /sdps trackall " .. config.track_all_units .. " |cffcccccc- 记录所有附近的单位|r")
      p("  /sdps mergepet " .. config.merge_pets .. " |cffcccccc- 合并宠物的数据到玩家|r")
      p("  /sdps texture " .. config.texture .. " |cffcccccc- 设置状态栏纹理|r")
      p("  /sdps pastel " .. config.pastel .. " |cffcccccc- 使用柔和的颜色|r")
      p("  /sdps backdrop " .. config.backdrop .. " |cffcccccc- 显示窗口背景和边框|r")
      p("  /sdps toggle |cffcccccc- 显示/隐藏 主窗口|r")
      return
    end

    local _, _, cmd, args = string.find(msg, "%s?(%w+)%s?(.*)")

    if strlower(cmd) == "visible" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.visible = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 可见性: "..config.visible.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-1]|r")
      end
    elseif strlower(cmd) == "toggle" then
      config.visible = config.visible == 1 and 0 or 1
      ShaguDPS_Config = config
      window.Refresh(true)
      p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 可见性: "..config.visible.."|r")
    elseif strlower(cmd) == "width" then
      if tonumber(args) then
        config.width = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 数据条宽度: "..config.width.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [1-999]|r")
      end
    elseif strlower(cmd) == "height" then
      if tonumber(args) then
        config.height = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 数据条高度: "..config.height.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [1-999]|r")
      end
    elseif strlower(cmd) == "spacing" then
      if tonumber(args) then
        config.spacing = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 数据条间距: "..config.spacing.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-"..config.height.."]|r")
      end
    elseif strlower(cmd) == "bars" then
      if tonumber(args) then
        config.bars = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 显示的数据条数: "..config.bars.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-999]|r")
      end
    elseif strlower(cmd) == "trackall" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.track_all_units = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 记录所有附近的单位: "..config.track_all_units.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-1]|r")
      end
    elseif strlower(cmd) == "mergepet" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.merge_pets = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 合并宠物数据: "..config.merge_pets.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-1]|r")
      end
    elseif strlower(cmd) == "texture" then
      if tonumber(args) and textures[tonumber(args)] then
        config.texture = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 纹理: "..config.texture.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [1-"..table.getn(textures).."]|r")
      end
    elseif strlower(cmd) == "pastel" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.pastel = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 使用柔和的颜色: "..config.pastel.."|r")
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-1]|r")
      end
    elseif strlower(cmd) == "backdrop" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.backdrop = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc 显示窗口的背景和边框: "..config.backdrop)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 有效值为 [0-1]|r")
      end
    end
  else
    -- GetLocale() == "enUS"
    if (msg == "" or msg == nil) then
      p("|cffffcc00Shagu|cffffffffDPS:")
      p("  /sdps visible " .. config.visible .. " |cffcccccc- Show main window")
      p("  /sdps width " .. config.width .. " |cffcccccc- Bar width")
      p("  /sdps height " .. config.height .. " |cffcccccc- Bar height")
      p("  /sdps spacing " .. config.spacing .. " |cffcccccc- Bar spacing")
      p("  /sdps bars " .. config.bars .. " |cffcccccc- Visible Bars")
      p("  /sdps trackall " .. config.track_all_units .. " |cffcccccc- Track all nearby units")
      p("  /sdps mergepet " .. config.merge_pets .. " |cffcccccc- Merge pets into owner data")
      p("  /sdps texture " .. config.texture .. " |cffcccccc- Set the statusbar texture")
      p("  /sdps pastel " .. config.pastel .. " |cffcccccc- Use pastel colors")
      p("  /sdps backdrop " .. config.backdrop .. " |cffcccccc- Show window backdrop and border")
      p("  /sdps toggle |cffcccccc- Toggle window")
      return
    end

    local _, _, cmd, args = string.find(msg, "%s?(%w+)%s?(.*)")

    if strlower(cmd) == "visible" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.visible = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Visible: " .. config.visible)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 0-1")
      end
    elseif strlower(cmd) == "toggle" then
      config.visible = config.visible == 1 and 0 or 1
      ShaguDPS_Config = config
      window.Refresh(true)
      p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Visible: " .. config.visible)
    elseif strlower(cmd) == "width" then
      if tonumber(args) then
        config.width = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Bar width: " .. config.width)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 1-999")
      end
    elseif strlower(cmd) == "height" then
      if tonumber(args) then
        config.height = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Bar height: " .. config.height)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 1-999")
      end
    elseif strlower(cmd) == "spacing" then
      if tonumber(args) then
        config.spacing = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Bar spacing: " .. config.spacing)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 0-" .. config.height)
      end
    elseif strlower(cmd) == "bars" then
      if tonumber(args) then
        config.bars = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Visible Bars: " .. config.bars)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 1-999")
      end
    elseif strlower(cmd) == "trackall" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.track_all_units = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Track all units: " .. config.track_all_units)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 0-1")
      end
    elseif strlower(cmd) == "mergepet" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.merge_pets = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Merge pet: " .. config.merge_pets)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 0-1")
      end
    elseif strlower(cmd) == "texture" then
      if tonumber(args) and textures[tonumber(args)] then
        config.texture = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Texture: " .. config.texture)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 1-" .. table.getn(textures))
      end
    elseif strlower(cmd) == "pastel" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.pastel = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Use pastel colors: " .. config.pastel)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 0-1")
      end
    elseif strlower(cmd) == "backdrop" then
      if tonumber(args) and (tonumber(args) == 1 or tonumber(args) == 0) then
        config.backdrop = tonumber(args)
        ShaguDPS_Config = config
        window.Refresh(true)
        p("|cffffcc00Shagu|cffffffffDPS:|cffffddcc Show window backdrop: " .. config.backdrop)
      else
        p("|cffffcc00Shagu|cffffffffDPS:|cffff5511 Valid Options are 0-1")
      end
    end
  end
end
